package com.qfedu.controller;

import com.qfedu.bean.Users;
import com.qfedu.service.UserService;
import com.sun.deploy.net.HttpResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;

/**
 * @author:zhengyilong
 * @email:1797344574@qq.com
 * @phone:13479394730
 * @desc:
 * @DateTime:2022/5/15 22:06
 **/
@Controller
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;

    @RequestMapping("/login")
    @ResponseBody
    public HashMap<String, Object> login(String name, String password, int typeId, HttpServletRequest request){
        HashMap<String,Object> map = new HashMap<String, Object>();
        Users user = userService.checkLogin(name, password);
        if (user == null){
            map.put("message","登录失败，用户不存在");
            return map;
        }
        if (user.getId() == 0){
            map.put("message","登录失败，密码错误");
            return map;
        }
        if (user.getStatus() != typeId){
            map.put("message","登录失败，用户身份类型错误");
            return map;
        }
        request.getSession().setAttribute("user",user);
        map.put("info",typeId);
        return map;
    }

    @RequestMapping("/insertUser")
    @ResponseBody
    public HashMap<String,Object> insrtUser(Users user){
        HashMap<String,Object> map = new HashMap<String, Object>();
        String s = userService.insertUser(user);
        if (s.equals("新增用户成功")){
            map.put("code",1);
        }
        map.put("info",s);
        return map;
    }

    @RequestMapping("/updateAdminUser")
    @ResponseBody
    public HashMap<String,Object> updateAdminUser(String name,String phone,String password){
        HashMap<String,Object> map = new HashMap<String, Object>();
        Users users = new Users();
        users.setName(name);
        users.setPhone(phone);
        String s = userService.updateAdminUser(users, password);
        if (s.equals("修改成功")){
            map.put("code",1);
        }
        map.put("info",s);
        return map;
    }

}

